﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace WindowsFormsApplication1
{
    public partial class Item : Form
    {
        public Item()
        {
            InitializeComponent();
        }

        private void txtiemCode_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                String getMax = "SELECT ISNULL(MAX(itemcode),0) FROM Items";
                SqlDataReader mmdr = DbConnet.ReadTable(getMax);
                String mx = "";

                if (mmdr.Read())
                {
                    mx = mmdr[0].ToString();
                }
                mmdr.Close();

                mx = mx.Substring(mx.IndexOf("I") + 1);

                int a = (int.Parse(mx) + 1);

                txtiemCode.Text = "I00" + a;
            }
            catch (Exception exietm)
            {
            }
        }

        private void Item_Load(object sender, EventArgs e)
        {
            DbConnet.connect(new Log().getConstr());
        }

        private void txtsup_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Popupform_suplier pfs = new Popupform_suplier();
            pfs.ShowDialog();
            txtsup.Text = new Midval().getSupName();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Boolean upd = false;
            Boolean ins = false;

            try
            {
                String supplier = txtsup.Text.Trim();
                supplier = supplier.Substring(0,supplier.IndexOf(" - "));
                String updItem = "UPDATE Items SET itemname = '" + txtitemName.Text.Trim() + "' , reorder = '" + txtreorder.Text.Trim() + "' , supplier  = '" + supplier + "',updatedby = '" + new Midval().getUser() + "' WHERE Itemcode = '" + txtiemCode.Text.Trim() + "' ";
                String insItem = "INSERT INTO Items (Itemcode,itemname,reorder,supplier,createdby,createddate) " +
                                 " VALUES ('" + txtiemCode.Text.Trim() + "','" + txtitemName.Text.Trim() + "','" + txtreorder.Text.Trim() + "','" + supplier + "','" + new Midval().getUser() + "','" + DateTime.Now + "')";

                upd = DbConnet.AddEditDel(updItem);

                if (!upd)
                    ins = DbConnet.AddEditDel(insItem);


                if (upd || ins)
                {
                    MessageBox.Show(new Form() { TopMost = true }, "Item Details Added Update Successfully ", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    txtiemCode.Clear();
                    txtitemName.Clear();
                    txtreorder.Clear();
                    txtsup.Clear();
                }
                
            }
            catch (Exception exsub)
            {
            }
        }

        private void txtiemCode_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                try
                {
                    String getDetails = "SELECT Itemcode,itemname,reorder,supplier FROM Items WHERE Itemcode = '" + txtiemCode.Text.Trim() + "'";
                    SqlDataReader ddr = DbConnet.ReadTable(getDetails);
                    String sup = "";

                    if (ddr.Read())
                    {
                        txtiemCode.Text = ddr["Itemcode"].ToString();
                        txtitemName.Text = ddr["itemname"].ToString();
                        txtreorder.Text = ddr["reorder"].ToString();
                        sup = ddr["supplier"].ToString();
                    }
                    ddr.Close();

                    String supName = "SELECT suppname FROM supplier WHERE Suppcode = '" + sup.Trim() + "'";
                    SqlDataReader ndr = DbConnet.ReadTable(supName);
                    if (ndr.Read())
                    {
                        txtsup.Text = ndr["suppname"].ToString();
                    }
                    ndr.Close();
                }
                catch (Exception exload)
                {
                }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Hide();

        }
    }
}
